import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/views/Home'
import Login from '@/views/Login/Login'
import Register from '@/views/Register/Register'
import About from '@/views/Main/About'
import Document from '@/views/Main/Document'
import Contact from '@/views/Main/Contact'
import ExamGrid from '@/views/ExamGrid/ExamGrid'
import UserMain from '@/views/User/UserMain'
import HomePage from '@/views/Main/HomePage'
import User from '@/views/User/User'
import MyQuest from '@/views/User/MyQuest'
import MyFavorite from '@/views/User/MyFavorite'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: 'HomePage',
      name: 'Home',
      component: Home,
      children: [
        {
          path: 'HomePage',
          name: 'HomePage',
          component: HomePage
        },
        {
          path: '/About',
          name: 'About',
          component: About
        },
        {
          path: '/Document',
          name: 'Document',
          component: Document
        },
        {
          path: '/Contact',
          name: 'Contact',
          component: Contact
        },
        {
          path: '/UserMain',
          name: 'UserMain',
          component: UserMain,
          children: [
            {
              path: '/User',
              name: 'User',
              component: User,
            },
            {
              path: '/MyQuest',
              name: 'MyQuest',
              component: MyQuest,
            },
            {
              path: '/MyFavorite',
              name: 'MyFavorite',
              component: MyFavorite,
            }

          ]
        },
        {
          path: '/Login',
          name: 'Login',
          component: Login
        },
        {
          path: '/Register',
          name: 'Register',
          component: Register
        },
        {
          path: '/ExamGrid',
          name: 'ExamGrid',
          component: ExamGrid
        }
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  let user = sessionStorage.getItem('user')
  if(to.path==='/Login'||to.path==='/About'||to.path==='/Document'
    ||to.path==='/HomePage'||to.path==='/Contact'||to.path==='/Register'
    ||to.path==='/ExamGrid'){
      next()
  }else {
    if (!user) {
    next({path: '/Login'})
    }else{
      next()
    }
  }
})
export default router
